import 'net_code.dart';

class NetReponse {
  final int code;
  final String msg;
  int? status;
  final dynamic data;

  bool get isSuccess {
    return code == 0;
  }

  NetReponse({
    required this.code,
    required this.msg,
    this.data,
    this.status,
  });

  factory NetReponse.fromJson(Map<String, dynamic> json) =>
      _$NetReponseFromJson(json);
}

NetReponse _$NetReponseFromJson(Map<String, dynamic> json) => NetReponse(
    code: json['code'] as int? ?? -99999,
    msg: json['message'] as String? ?? '',
    data: json['data'],
    status: json['status'] as int?);

class NetBaseReponse {
  String msg = '';
  int? status;
  dynamic data;
  late NetResponseCode code;

  bool get isSuccess {
    return code == NetResponseCode.success;
  }

  NetBaseReponse.response({
    required NetReponse res,
  }) {
    status = res.status;
    data = res.data;
    msg = res.msg;
    if (res.code == 1 || res.status == 200) {
      code = NetResponseCode.success;
    } else {
      switch (res.code) {
        case 401:
          //token 失效 清除token
          code = NetResponseCode.invalidToken;
          msg = '登录失效';
          break;
        default:
          code = NetResponseCode.unknow;
          if (msg.isEmpty) {
            msg = '未知错误';
          }

          break;
      }
    }
  }
  NetBaseReponse.code({required this.code}) {
    if (code == NetResponseCode.networkError) {
      msg = '网络异常，请重试';
    }
  }
  NetBaseReponse.origin({required this.data});
}
